//
//  TGDealCollectionViewCell.m
//  GroupBuy
//
//  Created by wwj on 15/11/9.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGDealCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "TGDealModel.h"

@interface TGDealCollectionViewCell ()
// 描述
@property (weak, nonatomic) IBOutlet UILabel *desc;
// 图片
@property (weak, nonatomic) IBOutlet UIImageView *image;
// 价格
@property (weak, nonatomic) IBOutlet UILabel *price;
// 购买人数
@property (weak, nonatomic) IBOutlet UIButton *purchaseCount;
// 标签
@property (weak, nonatomic) IBOutlet UIImageView *badge;
@end

@implementation TGDealCollectionViewCell

-(void)setModel:(TGDealModel *)model{
    _model = model;
    // 1.设置描述
    _desc.text = model.desc;
    
    // 2.下载图片
    [_image sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"placeholder_deal.png"]];
    
    // 3.购买人数
    [_purchaseCount setTitle:[NSString stringWithFormat:@"%d", model.purchase_count] forState:UIControlStateNormal];
    
    // 4.价格
    _price.text = model.current_price_text;
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *now = [fmt stringFromDate:[NSDate date]];
    
    NSString *icon = nil;
    if ([model.publish_date isEqualToString:now]) {
        icon = @"ic_deal_new.png";
    } else if ([model.purchase_deadline isEqualToString:now]) {
        icon = @"ic_deal_soonOver.png";
    } else if ([model.purchase_deadline compare:now] == NSOrderedAscending) {
        icon = @"ic_deal_over.png";
    }
    
    _badge.hidden = icon == nil;
    _badge.image = [UIImage imageNamed:icon];
}
@end
